﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.DAO.委托;
using LibrarySolution.DAO.基础;
using LibrarySolution.Model.基础;
using LibrarySolution.ComMethod;

namespace LibrarySolution.Frm
{
    public partial class FrmYhxxModify : Form
    {

        #region 属性
        Delegates delegates = new Delegates();
        YhDao yhxxDao = new YhDao();
        public static bool IsModify = true;
        private static FrmYhxxModify Instance;
        #endregion

        #region 构造器
        public FrmYhxxModify()
        {
            InitializeComponent();
            delegates.refreshLBEvent += new Delegates.refreshLBEventHandler(delegates_refreshLBEvent);
        }
        #endregion

        #region 事件
        public static FrmYhxxModify GetInstance(Delegates delegates)
        {
            Instance = new FrmYhxxModify();
            if (GlobalVar.YhEnter == false)
            {
                if (IsModify == false)
                    Instance.toolTip1.Dispose();
            }
            YhxxEntity yhxxEntity = new YhxxEntity();
            JbxxDetail jbxxDetail = new JbxxDetail();
            YhJbDao yhjbDao = new YhJbDao();
            Instance.CboYhJB.DataSource = yhjbDao.getDataTable(jbxxDetail);
            Instance.CboYhJB.DisplayMember = "级别名称";
            if (IsModify)
            {
                Instance.BindingData();
                Instance.CboYhJB.Text = GlobalVar.YhxxEntity.yh_JB;
                if (GlobalVar.YhxxEntity.yh_state == "1")
                {
                    Instance.panel1.Enabled = false;
                }

            }
            else
            {
                Instance.CboYhState.Items.RemoveAt(1);
                Instance.LblSOGZcsj.Text = DataUtil.getTime();
                Instance.LblSOGYhNo.Text = Instance.yhxxDao.getMaxYhNo();
                Instance.CboYhState.Text = Instance.CboYhState.Items[0].ToString();
                Instance.TxtYjsl.Enabled = false;
            }
            Instance.CboYhJB.GotFocus += new EventHandler(CboYhJB_GotFocus);
            Instance.delegates = delegates;
            return Instance;
        }

        static void CboYhJB_GotFocus(object sender, EventArgs e)
        {
            if (!IsModify && (GlobalVar.SpGly==true || GlobalVar.NoGly==true))
                SendKeys.Send("{F4}");
        }

        public static FrmYhxxModify GetInstance()
        {
            Instance = new FrmYhxxModify();
            YhxxEntity yhxxEntity = new YhxxEntity();
            Instance.BindingData();
            Instance.CboYhJB.Enabled = false;
            Instance.TxtXjsl.Enabled = false;
            Instance.TxtYjsl.Enabled = false;
            Instance.LblYhState.Visible = false;
            Instance.CboYhState.Visible = false;
            return Instance;
        }

        private void TstripBtnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TstripBtnDone_Click(object sender, EventArgs e)
        {
            if (TxtYhName.Text == "" && CboYhSex.Text == "" && TxtYjsl.Text == ""
                && TxtLxdh.Text == "" && CboYhJB.Text == "" && TxtYhZh.Text == "")
            {
                MessageBox.Show("必须输入完整信息");
                return;
            }
            if (TxtXjsl.Text == "")
                TxtXjsl.Text = "20";
            if (int.Parse(TxtXjsl.Text) + int.Parse(TxtYjsl.Text) > 20)
            {
                MessageBox.Show("最多只能续借" + (20 - int.Parse(TxtYjsl.Text)).ToString() + "本");
                return;
            }
            YhxxEntity yhxxEntity = new YhxxEntity();
            yhxxEntity.yh_no = LblSOGYhNo.Text;
            yhxxEntity.yh_zh = TxtYhZh.Text;
            yhxxEntity.yh_name = TxtYhName.Text;
            yhxxEntity.yh_sex = CboYhSex.Text;
            yhxxEntity.yhjb_no = yhxxDao.getYhjbNobyName(CboYhJB.Text);
            yhxxEntity.yh_zcsj = LblSOGZcsj.Text;
            yhxxEntity.yh_lxdh = TxtLxdh.Text;
            yhxxEntity.yh_yjsl = TxtYjsl.Text;
            yhxxEntity.yh_xjsl = TxtXjsl.Text;
            yhxxEntity.yh_pwd = TxtYhPwd.Text;
            if (CboYhState.Text == "是")
                yhxxEntity.yh_state = "0";
            else
                yhxxEntity.yh_state = "1";
            if (IsModify)
            {
                yhxxDao.UpdateYhxx(yhxxEntity);
                MessageBox.Show("修改成功");
            }
            else
            {
                String[] yhZh = yhxxDao.getYhZh();
                for (int i = 0; i < yhZh.Length; i++)
                {
                    if (TxtYhZh.Text == yhZh[i])
                    {
                        MessageBox.Show("账号已经存在");
                        return;
                    }
                }
                yhxxDao.insertYhxx(yhxxEntity);
                MessageBox.Show("注册成功");
            }
            if (Instance.delegates != null)
                delegates.doWork();
            this.Close();
        }

        private void FrmYhxxModify_Load(object sender, EventArgs e)
        {
            if (!IsModify)
            {
                CboYhJB.Text = "";
                CboYhSex.Text = CboYhSex.Items[0].ToString();
            }
            GlobalVar.YhEnter = true;
        }

        private void TxtXjsl_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == '\b'))
                e.Handled = true;
        }

        private void TxtYhPwd_Click(object sender, EventArgs e)
        {
            if (IsModify == true)
                FrmChangPwd.GetInstance(delegates).ShowDialog();
        }
        #endregion

        #region 方法
        void delegates_refreshLBEvent()
        {
            BindingData();
        }

        void BindingData()
        {
            TxtYhName.Text = GlobalVar.YhxxEntity.yh_name;
            LblSOGYhNo.Text = GlobalVar.YhxxEntity.yh_no;
            TxtYhZh.Text = GlobalVar.YhxxEntity.yh_zh;
            CboYhSex.Text = GlobalVar.YhxxEntity.yh_sex;
            CboYhJB.Text = GlobalVar.YhxxEntity.yh_JB;
            TxtYjsl.Text = GlobalVar.YhxxEntity.yh_yjsl;
            TxtXjsl.Text = GlobalVar.YhxxEntity.yh_xjsl;
            TxtLxdh.Text = GlobalVar.YhxxEntity.yh_lxdh;
            LblSOGZcsj.Text = GlobalVar.YhxxEntity.yh_zcsj;
            TxtYhPwd.Text = GlobalVar.YhxxEntity.yh_pwd;
            if (GlobalVar.YhxxEntity.yh_state == "0")
                CboYhState.Text = CboYhState.Items[0].ToString();
            else
                CboYhState.Text = CboYhState.Items[1].ToString();
            LblGetZhye.Text = GlobalVar.YhxxEntity.yh_zhye;
        }
        #endregion

        private void FrmYhxxModify_FormClosing(object sender, FormClosingEventArgs e)
        {
            GlobalVar.YhEnter = false;
        }
    }
}
